<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>商品(小)管理</title>
	<meta name="decorator" content="ani"/>
	<link rel="stylesheet" href="${ctxStatic}/plugin/layui-2.1.5/css/layui.css" media="all">
	<script type="text/javascript">

		$(document).ready(function() {
		    var jsonObj = {};
		    var detailSize = "${productDetail.productSize}";
		    // console.log(detailSize.length);
			if (detailSize != "" && detailSize.length > 0){
			    var str = "${productDetail.productSize}";
                jsonObj =  eval('(' + str + ')');
			}
		    var productSizeMap = ${productSizeMap};
		    // console.log(productSizeMap);
		    if (productSizeMap != null && typeof(productSizeMap) != 'undefined' && Object.keys(productSizeMap).length > 0){

                for(var item in productSizeMap){
                    var divHtml = "";
                    // console.log(item);
                    divHtml += "<div class=\"col-xs-12 col-sm-6 col-md-4 category\" style=\"width: 100%;display: flex;align-items: center;\">" +
                        "<label class=\"label-item single-overflow pull-left\" title=\""+item+"\" >"+item+"：</label>"
                    var jValue = productSizeMap[item];//key所对应的value
                    var selectHtml = "";
                    selectHtml+="<select name=\"category\" class=\"form-control\" style=\"width: 50%;\">";
                    for(var i = 0 ; i < jValue.length ; i++){
                        var value = jValue[i];
						selectHtml += "<option value=\""+value+"\"";
						if (Object.keys(jsonObj).length > 0){
                            if (jsonObj[item] == value){
                                selectHtml += " selected ";
							}
						}
						selectHtml += ">"+value+"</option>";
                    }
                    selectHtml+= "</select></div>";
                    divHtml += selectHtml;
                    $("#sizeDiv").append(divHtml);

                }
			}else{
                $("#sizeDiv").append("<div>暂无规格，返回规格配置进行规格的配置</div>");
			}


			$("#inputForm").validate({
				submitHandler: function(form){
                    var json = {};
                    var categoryArray = $('.category');
                    for(var i = 0 ;i < categoryArray.length ; i ++){
						var divArray = categoryArray[i];
						var label = divArray.getElementsByTagName("label");
						var select = divArray.getElementsByTagName("select");
                        json[label[0].title] = select[0].value;

					}
                    $("#productSize").val(JSON.stringify(json).replace(/\"/g,"\'"));
                    // console.log($("#productSize").val());
                    if ($("#productSize").val() == '{}'){
                        layer.open({
                            title: '提示'
                            ,content: '暂无规格，返回规格配置进行规格的配置'
                        });
                        return false;
					}
					jp.loading();
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
		});
	</script>
</head>
<body>
<div class="wrapper wrapper-content">				
<div class="row">
	<div class="col-md-12">
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h3 class="panel-title"> 
				<a class="panelButton" href="${ctx}/anny/prodcuct/productDetail?productId=${productDetail.productId}"><i class="ti-angle-left"></i> 返回</a>
			</h3>
		</div>
		<div class="panel-body">
		<form:form id="inputForm" modelAttribute="productDetail" action="${ctx}/anny/prodcuct/productDetail/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<form:hidden path="productId"/>
		<form:hidden path="productSize"/>
		<sys:message content="${message}"/>
			<table class="layui-table" >
				<colgroup>
					<col width="18%">
					<col width="32%">
					<col width="18%">
					<col width="32%">
				</colgroup>
				<tbody>

					<tr>
						<th><font color="red">*</font>规格：</th>
						<td id="sizeDiv">
						</td>

						<th><font color="red">*</font>价钱：</th>
						<td>
							<form:input path="price" htmlEscape="false"  id="price"  class="form-control required"/>
						</td>
					</tr>
					<tr>
						<th><font color="red">*</font>库存：</th>
						<td>
							<form:input path="inventory" htmlEscape="false"  id="inventory"   class="form-control required"/>
						</td>
						<th>购买商品所需积分：</th>
						<td>
							<form:input path="evaluatePrice" htmlEscape="false"  id="evaluatePrice"   class="form-control "/>
						</td>
					</tr>

					<tr>
						<th><font color="red">*</font>购买商品所获得积分：</th>
						<td>
							<form:input path="evaluate" htmlEscape="false"  id="evaluate"   class="form-control required"/>
						</td>
						<th>备注信息：</th>
						<td>
							<form:input path="remarks" htmlEscape="false"  id="remarks"   class="form-control "/>
						</td>
					</tr>

				</tbody>
			</table>

				<div class="col-lg-3"></div>
		        <div class="col-lg-6">
		             <div class="form-group text-center">
		                 <div>
		                     <button class="btn btn-primary btn-block btn-lg btn-parsley" data-loading-text="正在提交...">提 交</button>
		                 </div>
		             </div>
		        </div>
		</form:form>
		</div>				
	</div>
	</div>
</div>
</div>
</body>
</html>